<?php
/***
 * ActionLeaf (www.actionleaf.com / actionleaf.googlecode.com) PHP Framework
 * Copyright (c) 2008, Wess D. Cope <wess@wattz.net>
 * All rights reserved.								
 * 													
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:				   
 * 																				
 * 	* Redistributions of source code must retain the above copyright notice,	
 * 	this list of conditions and the following disclaimer.						
 * 																				
 * 	* Redistributions in binary form must reproduce the above copyright notice, 
 * 	this list of conditions and the following disclaimer in the documentation   
 * 	and/or other materials provided with the distribution. 						
 * 																				
 * 	* Neither the name of the <ORGANIZATION> nor the names of its contributors may
 * 	be used to endorse or promote products derived from this software without	 
 * 	specific prior written permission.											
 * 																				
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 	 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES  
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 	 
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 		 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 	 
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 	 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 	 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 	 
 * DAMAGE.																				 
 **/


/**
 * Foundation.ALDispatch
 * ALDispatch is used to process request url based on the routes.conf
 * and call page action delegate.
 */

function ALDispatch()
{
	//$routes = ALFormatParser::json(ROOT_DIR . '/system/conf/routes.conf');
	$routes = ALFormatParser::yaml(ROOT_DIR . '/system/conf/routes.conf');
	$url = isset($_GET['_url'])? $_GET['_url'] : '';
	
	$requestObject = new stdClass;
	$requestObject->requestUri = $_SERVER['REQUEST_URI'];
	$requestObject->host = $_SERVER['HTTP_HOST'];
	$requestObject->requestTime = $_SERVER['REQUEST_TIME'];
	$requestObject->url = $url;
	$requestObject->params = array_merge($_GET, $_POST);
	
	$route = ALRoute($requestObject, $routes, $url);

	if ($route === null) {

		die("No route! The interweb has been hacked! Alert the NSA! Call asonge for a fix for the erlang interweb 2.0!!!!");

	}

	list($dir, $action) = explode('.', $route);

	//if(!file_exists(ROOT_DIR . '/apps/' . $dir . '/views.php'))
	//	die('500 error');
				
	require(ROOT_DIR . '/apps/' . $dir . '/views.php');
			
	$method = 'do' . $_SERVER['REQUEST_METHOD'];
	$page = new $action();
	$settings =  ALFormatParser::yaml(ROOT_DIR . '/system/conf/setup.conf');
	
	if(!$settings['useSessions'])
		session_destroy();

	if($settings['useTemplates'])
	{
		ALFileLoad::lib(ALViewProvider);
		$requestObject->view = new ALViewProvider(ROOT_DIR . 'apps/' . $settings['templateDirectory'] . '/');
	}
	
	die($page->$method($requestObject));

}

function ALRoute ($request, $routes, $path)
{

	for ($i = 0, $icount = count($routes); $i < $icount; $i++)
	{

		$key = $routes[$i]['url'];

		if (preg_match("#{$key}#", $path, $args))
		{

			if (count($args) > 1)
			{

				// we have more matched groups
				foreach ($args as $name => $value)
				{

					if (!is_int($name))
					{

						// set matched group as request parameter
						$request->params[$name] = $value;

					}

				}

			}
			
			if (isset($routes[$i]['include'])) 
			{
				$route_conf_path = str_replace(".", "/", $routes[$i]['include']);
				return ALRoute(
						$request, 
						ALFormatParser::yaml(ROOT_DIR . $route_conf_path . '.conf'), 
						substr($path, strlen($args[0]))
					);

			}

			$file = $routes[$i]['action'];

			if (is_array($file))
			{

				return ALRoute($request, $file, substr($path, strlen($args[0])));

			}

			return $file;

		}

	}
	
	return null;

}
